package com.vessel.decoration.mvp.presenter.mine;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.vessel.decoration.app.data.entity.NotePadResponse;
import com.vessel.decoration.mvp.contract.mine.NotePadContract;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

/**
 * @author vesselzhang
 * @date 2017/12/5
 */

@ActivityScope
public class NotePadPresenter extends BasePresenter<NotePadContract.Model, NotePadContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private AppManager mAppManager;

    @Inject
    public NotePadPresenter(NotePadContract.Model model, NotePadContract.View rootView
            , RxErrorHandler handler, Application application, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mAppManager = appManager;
    }

    public void queryNotepad() {
        mModel.queryNotepad()
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<NotePadResponse>(mErrorHandler) {
                    @Override
                    public void onNext(NotePadResponse notePadResponse) {
                        mRootView.setContent(notePadResponse.getContent());
                    }
                });
    }

    public void saveNotepad(String content) {
        mModel.saveNotepad(content)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<Boolean>(mErrorHandler) {
                    @Override
                    public void onNext(Boolean aBoolean) {
                        mRootView.showMessage("保存成功");
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mApplication = null;
    }
}